/*
 * Copyright (c) 2017.  . All rights reserved.
 */

package com.yiqi.zhiyuan.common.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.zhiyuan.feature.msg.view.MessageActivity;

import cn.jpush.android.api.JPushInterface;


public class JPushReceiver extends BroadcastReceiver {
    public static final String TAG = "JPUSH";

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        Logger.d("收到极光推送广播-------->>>>> Action = " + action);

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(action)) {
            String regId = intent.getStringExtra(JPushInterface.EXTRA_REGISTRATION_ID);
            Logger.d("[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(action)) {
            String jsonMsg = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);


        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(action)) {
            int notifactionId = intent.getIntExtra(JPushInterface.EXTRA_NOTIFICATION_ID, -1);
            Logger.d("接收到推送下来的通知的ID: " + notifactionId);
            processCustomMessage(context, intent);


        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(action)) {
            Logger.d("[MyReceiver] 用户点击打开了通知");
            openNotification(context, intent);

        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(action)) {
            Logger.d("[JpushReceiver] 用户收到到RICH PUSH CALLBACK: " + intent.getStringExtra(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(action)) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Logger.d("[JpushReceiver]" + " 连接状态变化 connected = " + connected);

        } else {
            Logger.d("[JpushReceiver] Unhandled intent - " + action);
        }
    }

    /**
     * 通知打开
     *
     * @param context
     * @param bundle
     */
    private void openNotification(Context context, Intent bundle) {

        String title = bundle.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        Logger.e(TAG + " title : " + title);
        String message = bundle.getStringExtra(JPushInterface.EXTRA_ALERT);
        Logger.e(TAG + "message : " + message);
        String extras = bundle.getStringExtra(JPushInterface.EXTRA_EXTRA);
        Logger.e(TAG + "extras : " + extras);


        Intent intent = new Intent(context, MessageActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);


    }

    /**
     * 收到自定义消息
     *
     * @param context
     * @param
     */
    private void processCustomMessage(Context context, Intent bundle) {
        //{"type":1,"content":{"key1":value1,"key2":"value2"}}
        Logger.e("处理推送的自定义消息： ");

        String title = bundle.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        Logger.e(TAG + " title : " + title);
        String message = bundle.getStringExtra(JPushInterface.EXTRA_ALERT);
        Logger.e(TAG + "message : " + message);
        String extras = bundle.getStringExtra(JPushInterface.EXTRA_EXTRA);
        Logger.e(TAG + "extras : " + extras);


        bookingConditionTask(context, null);

    }

    /**
     * 待办事项任务处理
     *
     * @param context
     * @param msg
     */
    private void bookingConditionTask(Context context, String msg) {
//        JpushHelper.newTaskTips(msg);

        //把要发送的广播值传入Intent对象
        Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
        //调用Context的 sendBroadcast()方法发送广播
        context.sendBroadcast(intent);
    }


}
